﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
//class A
//{
//public:
//	virtual ~A()
//	{
//		cout << "~A" << endl;
//	}
//};
//class B : public A
//{
//public:
//	virtual ~B()
//	{
//		cout << "~B" << endl;
//	}
//protected:
//	int* _p = new int[10];
//};
//
//int main()
//{
//	A* p1 = new A;
//	A* p2 = new B;
//
//	delete p1;
//	delete p2;
//
//	return 0;
//}

class Person
{
public:
	virtual void piao() 
	{
		cout << "cheng" << endl;
	}
	
};

class Student : public Person
{
public:
	virtual void piao()	override
	{
		cout << "xue" << endl;
	}
};

void Func(Person& p)
{
	p.piao();
}

//int main()
//{
//	Person p;
//	Student s;
//
//	Func(p);
//	Func(s);
//}
#include <iostream>
using namespace std;

// 抽象类 Shape
class Shape {
public:
    virtual void draw() = 0;  // 纯虚函数
};

// 派生类 Circle
class Circle : public Shape {
public:
    void draw() override {
        cout << "Draw Circle" << endl;
    }
};

// 派生类 Rectangle
class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Draw Rectangle" << endl;
    }
};

int main() {
    Shape* shape1 = new Circle();
    Shape* shape2 = new Rectangle();

    shape1->draw();  // 输出: Draw Circle
    shape2->draw();  // 输出: Draw Rectangle

    delete shape1;
    delete shape2;
    return 0;
}

class Person 
{
public:
    virtual void BuyTicket() 
    {
        cout << "买票全价" << endl; 
    }
};

class Student : public Person 
{    
public:
    virtual void BuyTicket() 
    {
        cout << "买票打折" << endl; 
    }
   
};

class Soldier : public Person
{
public:
     virtual void BuyTicket() 
     {
         cout << " 买票优先" << endl; 
     }
};

void Func(Person* ptr)
{
   //  这⾥可以看到虽然都是Person指针Ptr在调⽤BuyTicket
   //  但是跟ptr没关系，⽽是由ptr指向的对象决定的。

    ptr->BuyTicket();

}
            
int main()
{
 
    // 其次多态不仅仅发⽣在派⽣类对象之间，多个派⽣类继承基类，重写虚函数后
    // 多态也会发⽣在多个派⽣类之间。
            Person ps;
            Student st;
            Soldier sr;
            Func(&ps);
            Func(&st);
            Func(&sr);
            return 0;
}